package com.yuexun.dhdoctor.module.personalPlan.ui;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.baselistener.OnDataBackListener;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.personalPlan.bean.CreatePlanBean;
import com.yuexun.dhdoctor.module.personalPlan.bean.UpdatePlanBean;
import com.yuexun.dhdoctor.module.personalPlan.model.impl.PlanEdtModel;
import com.yuexun.dhdoctor.module.personalPlan.presenter.impl.PlanEdtPresenter;
import com.yuexun.dhdoctor.module.personalPlan.ui.view.IPlanEdtView;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/9/7.
 */
public class Act_PlanEdt extends BaseKJActivity implements IPlanEdtView {
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.planedt_date_tv)
    TextView planedtDateTv;
    @Bind(R.id.planedt_time_tv)
    TextView planedtTimeTv;
    @Bind(R.id.planedt_content_edt)
    EditText planedtContentEdt;

    Boolean isCreate = false;

    PlanEdtPresenter presenter;
    String date;
    OnDataBackListener listener;

    public void setListener(OnDataBackListener listener) {
        this.listener = listener;
    }

    @Override
    public void setRootView() {
        setContentView(R.layout.act_planedt);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new PlanEdtPresenter(new PlanEdtModel(),this);
        date = getIntent().getExtras().getString("date").split(" ")[0];
        try {
            planedtDateTv.setText(new SimpleDateFormat("MM-dd").format(new SimpleDateFormat("yyyy-MM-dd").parse(date))
            + " " + getIntent().getExtras().getString("date").split(" ")[1]);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        planedtTimeTv.setText(getIntent().getExtras().getString("time"));
        planedtContentEdt.setText(getIntent().getExtras().getString("content"));
        planedtContentEdt.setSelection(planedtContentEdt.getText().length());
        if(getIntent().getExtras().getBoolean("iscreate"))
        {
            isCreate =false;
            title.setText("修改计划");
        }
        else {
            isCreate =true;
            title.setText("添加计划");
        }
    }

    @OnClick({R.id.taskedt_left, R.id.taskedt_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                break;
            case R.id.taskedt_left:
                finish();
                break;
            case R.id.taskedt_right:
                if (TextUtils.isEmpty(planedtContentEdt.getText().toString())) {
                    ViewInject.toast("请填写计划内容");
                    break;
                }
                if(isCreate) {
                    Map<String, String> params = new HashMap<>();
                    params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                    params.put("startDatetime", date);
                    params.put("ampm", (planedtTimeTv.getText().toString().equals("上午")) ? "1" : "2");
                    params.put("content", planedtContentEdt.getText().toString());
                    presenter.createMyTask(params);
                }
                else {
                    Map<String, String> params = new HashMap<>();
                    params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                    params.put("personalScheduleId", getIntent().getExtras().getString("personalScheduleId"));
                    params.put("content", planedtContentEdt.getText().toString());
                    presenter.updateMyTask(params);
                }
                break;
        }
    }


    @Override
    public void createTask(CreatePlanBean createPlanBean) {
        if(createPlanBean.getStatus().equals("200"))
        {
//            PlanBean planBean = new PlanBean();
//            planBean.setAmpm(planedtTimeTv.getText().toString());
//            planBean.setContent(planedtContentEdt.getText().toString());
//            planBean.setStartDate(planedtDateTv.getText().toString().split(" ")[0]);
//            planBean.setPersonalScheduleId(createPlanBean.getDatas().get(0).getPersonalScheduleId());
//
//            Bundle bundle = new Bundle();
//            bundle.putString("type", "200");
//            bundle.putString("key", getIntent().getExtras().getString("key"));
//            bundle.putSerializable("planBean", planBean);

//            if(listener != null){
//                listener.showData(bundle);
//            }

//            Intent intent = new Intent();
//            bundle.putSerializable("key", getIntent().getExtras().getString("key"));
//            bundle.putSerializable("startDatetime", planedtDateTv.getText().toString().split(" ")[0]);
//            bundle.putSerializable("ampm", planedtTimeTv.getText().toString());
//            bundle.putSerializable("content", planedtContentEdt.getText().toString());
//            bundle.putSerializable("personalScheduleId", createPlanBean.getDatas().get(0).getPersonalScheduleId());
//            intent.putExtras(bundle);
//            setResult(200, intent);
            ViewInject.toast("创建成功");
            finish();
        }
        else {
            ViewInject.toast("创建失败");
        }
    }

    @Override
    public void updateTask(UpdatePlanBean updatePlanBean) {
        if(updatePlanBean.getStatus().equals("200"))
        {
//            Bundle bundle = new Bundle();
//            bundle.putString("type", "201");
//            bundle.putString("key", getIntent().getExtras().getString("key"));
//            bundle.putSerializable("content", planedtContentEdt.getText().toString());
//
//            if(listener != null){
//                listener.showData(bundle);
//            }
            finish();
        }
        else {
            ViewInject.toast("编辑失败");
        }
    }
}
